#ifndef CT_RWLOCK_H_
#define CT_RWLOCK_H_

#include <pthread.h>
#include "ct_noncopyable.h"

class ctRWLock: private ctNoncopyable
{
        public:
                ctRWLock()
                {
                        ::pthread_rwlock_init(&rwlock_, NULL);
                }
                ~ctRWLock()
                {
                        ::pthread_rwlock_destroy(&rwlock_);
                }
                inline void rdlock()
                {
                        ::pthread_rwlock_rdlock(&rwlock_);
                }
                inline void wrlock()
                {
                        ::pthread_rwlock_wrlock(&rwlock_);
                }
                inline void unlock()
                {
                        ::pthread_rwlock_unlock(&rwlock_);
                }
        private:
                pthread_rwlock_t rwlock_;
};

class ctRWLock_scope_rdlock:private ctNoncopyable
{
        public:
                ctRWLock_scope_rdlock(ctRWLock &l)
                        : rwlock_(l)
                {
                        rwlock_.rdlock();
                }
                ~ctRWLock_scope_rdlock()
                {
                        rwlock_.unlock();
                }
        private:
                ctRWLock &rwlock_;
};

class ctRWLock_scope_wrlock:private ctNoncopyable
{
        public:
                ctRWLock_scope_wrlock(ctRWLock &l)
                        : rwlock_(l)
                {
                        rwlock_.wrlock();
                }
                ~ctRWLock_scope_wrlock()
                {
                        rwlock_.unlock();
                }
        private:
                ctRWLock &rwlock_;
};

#endif
